﻿var CloudTreeList = [];
function RenderTree() {
    //获取所有cloudtree标签的对象
    var cloudTreeList = $("cloud-tree");
    for (var i = 0; i < cloudTreeList.length; i++) {
        var cloudTree = cloudTreeList[i];
        var id = "cloud_" + $(cloudTree).attr("id");
        var url = $(cloudTree).attr("url");
        var selectvalue = $(cloudTree).attr("selectvalue");
        //select/selectgroup/selectou/selectmodule/selectcode
        var controltype = $(cloudTree).attr("control-type");
        //single/multi/
        var selecttype = $(cloudTree).attr("select-type");
        //自定义快速数据源时的参数
        //分组树
        var grouptag = $(cloudTree).attr("grouptag");
        //代码项树
        var mainguid = $(cloudTree).attr("mainguid");
        var nodeclick = $(cloudTree).attr("nodeclick");
        var codeid = id + "_code";
        var treecontainerid = id + "_ztree_cotainer"
        var treeid = id + "_ztree"
        var text = "";
        text += " <input id=" + id + "  class=\"cloudtree-select\"   readonly > ";
        text += "    <!-- 模拟select点击框 以及option的text值显示-->";
        text += "    <i class=\"cloudtree-drop\" ></i>";
        text += "    <!-- 模拟select右侧倒三角 -->";
        text += "    <input id=" + codeid + " type=\"hidden\"  />";
        text += "    <!-- zTree树状图 相对定位在其下方 -->";
        text += "    <div id=" + treecontainerid + "  class=\"cloudtree-container\">";
        text += "        <ul class=\"ztree\" id=" + treeid + "></ul>";
        text += "    </div>  ";
        cloudTree.innerHTML = text;
        var CloudTreeObj = { "id": id, "treeid": treeid, "controltype": controltype, "selecttype": selecttype, "grouptag": grouptag, "mainguid": mainguid };
        if (selectvalue && selectvalue != "") {
            CloudTreeObj.selectvalue = selectvalue;
        }
        CloudTreeList.push(CloudTreeObj);
        $("#" + id).click(function () {
            ShowTree(id);
        })
        InitTree(id, url, nodeclick);
        $("#" + treecontainerid).css("width", $("#" + id).outerWidth())
    }
}

function InitTree(ID, TreeDataUrl, TreeOnClick) {
    var TreeID = ID + "_ztree"
    var TreeContainerID = ID + "_ztree_cotainer"
    var CloudTreeObj = GetTreeObj(ID)
    var setting = {
        data: {
            simpleData: {
                enable: true,  //true 、 false 分别表示 使用 、 不使用 简单数据模式 
                idKey: "id",   //节点数据中保存唯一标识的属性名称
                pIdKey: "pid",    //节点数据中保存其父节点唯一标识的属性名称 
                rootPId: -1,  //用于修正根节点父节点数据，即 pIdKey 指定的属性值
            },
            key: {
                name: "name"  //zTree 节点数据保存节点名称的属性名称  默认值："name"
            },
        },
        check: {

        },
        callback: {

        }
    };
    if (CloudTreeObj.selecttype == "single") {
        setting.check.enable = true;
        setting.check.chkStyle = "radio";
        setting.check.radioType = "all";
    }
    if (TreeOnClick) {
        setting.callback.onClick = TreeOnClick
    } else {
        if (CloudTreeObj.selecttype == "single") {
            setting.callback.onClick = function (event, treeId, treeNode, clickFlag) {
                var treeObj = $.fn.zTree.getZTreeObj(TreeID);
                var nodes = treeObj.getSelectedNodes();
                for (var i = 0, l = nodes.length; i < l; i++) {
                    if (nodes[i].id == treeNode.id) {
                        treeObj.checkNode(nodes[i], true, true);
                    }
                }
                CloudTreeObj.selectvalue = treeNode.id;
                CloudTreeObj.selectname = treeNode.name;
                $(treeObj).attr("selectvalue", treeNode.id)
                $("#" + ID).val(treeNode.name);
                HideTree(ID)
            }
            setting.callback.onCheck = function (event, treeId, treeNode, clickFlag) {
                var treeObj = $.fn.zTree.getZTreeObj(TreeID);
                CloudTreeObj.selectvalue = treeNode.id;
                CloudTreeObj.selectname = treeNode.name;
                $(treeObj).attr("selectvalue", treeNode.id)
                $("#" + ID).val(treeNode.name);
                HideTree(ID)
            }
        }

    }
    if (CloudTreeObj.controltype == "selectgroup") {
        CloudPost({ "GroupTag": CloudTreeObj.grouptag }, GetRootPath() + "frame/base/group/grouptree", function (res) {
            if (res.code == 0) {
                var TreeNode = res.data
                zTreeObj = $.fn.zTree.init($("#" + TreeID), setting, TreeNode);
                CloudTreeObj.zTreeObj = zTreeObj;
                if (CloudTreeObj.selectvalue && CloudTreeObj.selectvalue != "") {
                    SetNodeChecked(ID);
                }
            }
        })
    }
    else if (CloudTreeObj.controltype == "selectou") {
        CloudPost({}, GetRootPath() + "frame/org/ou/tree", function (res) {
            if (res.code == 0) {
                var TreeNode = res.data
                zTreeObj = $.fn.zTree.init($("#" + TreeID), setting, TreeNode);
                CloudTreeObj.zTreeObj = zTreeObj;
                if (CloudTreeObj.selectvalue && CloudTreeObj.selectvalue != "") {
                    SetNodeChecked(ID);
                }
            }
        })
    }
    else if (CloudTreeObj.controltype == "selectmodule") {
        CloudPost({}, GetRootPath() + "frame/org/module/tree", function (res) {
            if (res.code == 0) {
                var TreeNode = res.data
                zTreeObj = $.fn.zTree.init($("#" + TreeID), setting, TreeNode);
                CloudTreeObj.zTreeObj = zTreeObj;
                if (CloudTreeObj.selectvalue && CloudTreeObj.selectvalue != "") {
                    SetNodeChecked(ID);
                }
            }
        })
    }
    else if (CloudTreeObj.controltype == "selectcode") {
        CloudPost({ "MainGuid": CloudTreeObj.mainguid }, GetRootPath() + "frame/dmp/codeitem/tree", function (res) {
            if (res.code == 0) {
                var TreeNode = res.data
                zTreeObj = $.fn.zTree.init($("#" + TreeID), setting, TreeNode);
                CloudTreeObj.zTreeObj = zTreeObj;
                if (CloudTreeObj.selectvalue && CloudTreeObj.selectvalue != "") {
                    SetNodeChecked(ID);
                }
            }
        })
    }
    else {
        CloudPost({}, TreeDataUrl, function (res) {
            if (res.code == 0) {
                var TreeNode = res.data
                zTreeObj = $.fn.zTree.init($("#" + TreeID), setting, TreeNode);
                CloudTreeObj.zTreeObj = zTreeObj;
                if (CloudTreeObj.selectvalue && CloudTreeObj.selectvalue != "") {
                    SetNodeChecked(ID);
                }
            }
        })
    }

}

function ShowTree(ID) {
    var TreeContainerID = ID + "_ztree_cotainer"
    $('#' + TreeContainerID).css('display', 'block');
    $("body").bind("mousedown", function (event) {
        if (event.target.id.indexOf(ID) == -1 && event.target.id.indexOf(TreeContainerID) == -1) {
            if (event.target.id != 'selectDevType') {
                HideTree(ID);
            }
        }
    });

}

function HideTree(ID) {
    var TreeContainerID = ID + "_ztree_cotainer"
    $("#" + TreeContainerID).css('display', 'none');
    $("body").unbind("mousedown");
    return false;
}

function GetTreeObj(ID) {
    var CloudTreeObj;
    for (var i = 0; i < CloudTreeList.length; i++) {
        if (CloudTreeList[i].id == ID) {
            CloudTreeObj = CloudTreeList[i];
        }
    }
    return CloudTreeObj;
}

function GetTreeSelectValue(ID) {
    return GetTreeObj("cloud_" + ID).selectvalue;
}

function SetNodeChecked(ID) {
    var CloudTreeObj = GetTreeObj(ID);
    var TreeID = ID + "_ztree"
    var treeObj = $.fn.zTree.getZTreeObj(TreeID);
    var nodes = treeObj.getNodesByParam("id", CloudTreeObj.selectvalue, null);
    for (var i = 0, l = nodes.length; i < l; i++) {
        $("#" + ID).val(nodes[i].name)
        treeObj.checkNode(nodes[i], true, true);
    }
}
